package com.zxj.shop.admin.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxj.shop.admin.entity.Permission;
import com.zxj.shop.admin.entity.Role;
import com.zxj.shop.admin.service.PermissionService;
import com.zxj.shop.admin.service.RoleService;
import com.zxj.shop.admin.shiro.vo.PageBean;
import com.zxj.shop.admin.shiro.vo.PageVO;
import com.zxj.shop.admin.shiro.vo.ResultVO;
import com.zxj.shop.admin.shiro.vo.RolePermissionParam;
import lombok.val;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/api/admin/setting/rolePermission")
public class RolePermissionApiController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @PostMapping("/roles")
    public ResultVO<List<Role>> roles() {
        return ResultVO.success(roleService.getRoles());
    }

    @GetMapping("/list")
    public PageVO<Role> list(Role role, PageBean pageBean) {
        IPage<Role> rolePage = roleService.page(new Page<>(pageBean.getPage(), pageBean.getLimit()), role);
        return PageVO.pageVO(rolePage.getRecords(), rolePage.getTotal());
    }

    @RequiresPermissions("adsfasdfafds")
    @PostMapping("/edit")
    public ResultVO<Role> edit(Integer id) {
        Role sysRole = roleService.getRoleById(id);
        return ResultVO.success(sysRole);
    }

    @PostMapping("/editPermission")
    public List<RolePermissionParam> editPermission(Integer roleId) {
        val sysPermissionList = permissionService.getUserPermission(roleId.toString());
        List<Integer> menuIdList = new ArrayList<>();
        for (Permission sysPermission : sysPermissionList) {
            menuIdList.add(sysPermission.getId());
        }
        return set(permissionService.list(), "0", true, menuIdList);
    }

    /**
     * 轮训获取对应的权限
     *
     * @param resourceList 权限列表
     * @param superId      父级
     * @param spread       是否展开
     * @param menuIdList   拥有的权限id集合
     * @return 权限列表
     */
    private List<RolePermissionParam> set(List<Permission> resourceList, String superId, Boolean spread, List<Integer> menuIdList) {
        val list = new ArrayList<RolePermissionParam>();
        for (Permission sysResource : resourceList) {
            if (superId.equals(sysResource.getPid().toString())) {
                val param = new RolePermissionParam();
                param.setTitle(sysResource.getName());
                param.setId(sysResource.getId());
                param.setSpread(spread);
                param.setDisabled(false);
                val rolePermissionParams = set(resourceList, sysResource.getId().toString(), spread, menuIdList);
                if (CollUtil.isNotEmpty(rolePermissionParams)) {
                    param.setChildren(rolePermissionParams);
                } else {
                    param.setChildren(new ArrayList<>());
                    // 判断是否属于已经存在
                    param.setChecked(CollUtil.isNotEmpty(menuIdList) && menuIdList.contains(sysResource.getId()));
                }
                list.add(param);
            }
        }
        return list;
    }

}
